package com.hualan.sfzy.service.counselor.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.controller.counselor.CounselorPriceClassifyController;
import com.hualan.sfzy.controller.counselor.CounselorPriceStandardController;
import com.hualan.sfzy.entity.counselor.CounselorOrder;
import com.hualan.sfzy.entity.counselor.CounselorPriceClassify;
import com.hualan.sfzy.entity.counselor.CounselorPriceStandard;
import com.hualan.sfzy.mapper.counselor.CounselorPriceClassifyMapper;
import com.hualan.sfzy.mapper.counselor.CounselorPriceStandardMapper;
import com.hualan.sfzy.service.counselor.CounselorPriceClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("CounselorPriceClassifyService")

public class CounselorPriceClassifyServiceImpl implements CounselorPriceClassifyService {
    @Autowired
    private CounselorPriceClassifyMapper counselorPriceClassifyMapper;
    @Autowired
    private CounselorPriceStandardMapper counselorPriceStandardMapper;

    @Override
    public int add(CounselorPriceClassify counselorPriceClassify) {
        return counselorPriceClassifyMapper.insert(counselorPriceClassify);
    }

    @Transactional
    @Override
    public int remove(Integer id) {
        CounselorPriceStandard counselorPriceStandard = new CounselorPriceStandard();
        counselorPriceStandard.setPriceClassifyId(id);
        List<CounselorPriceStandard> list = counselorPriceStandardMapper.select(counselorPriceStandard);
        if(list.size() > 0){
            throw new RuntimeException("收费标准管理中存在收费管理");
        }
        return counselorPriceClassifyMapper.delete(id);
    }

    @Transactional
    @Override
    public int removeBatch(Integer[] ids) {
        CounselorPriceStandard counselorPriceStandard = new CounselorPriceStandard();
        for(Integer id : ids){
            counselorPriceStandard.setPriceClassifyId(id);
            List<CounselorPriceStandard> list = counselorPriceStandardMapper.select(counselorPriceStandard);
            if(list.size() > 0){
                throw new RuntimeException("收费标准管理中存在收费管理");
            }
        }
        return counselorPriceClassifyMapper.deleteBatch(ids);
    }

    @Override
    public int edit(CounselorPriceClassify counselorPriceClassify) {
        return counselorPriceClassifyMapper.update(counselorPriceClassify);
    }

    @Override
    public List<CounselorPriceClassify> find() {
        return counselorPriceClassifyMapper.select(null);
    }

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, CounselorPriceClassify entity) {
        PageHelper.startPage(pageNum,pageSize);

        List<CounselorPriceClassify> list = counselorPriceClassifyMapper.select(entity);

        PageInfo<CounselorPriceClassify> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();

        map.put(CommonConstant.PAGE_LIST,list);
        map.put(CommonConstant.PAGE_TOTAL,pageInfo.getPages());
        map.put(CommonConstant.TOTAL,pageInfo.getTotal());

        return map;
    }

    @Override
    public CounselorPriceClassify findById(Integer id) {
        return counselorPriceClassifyMapper.selectById(id);
    }


}
